import axios from 'axios';
import { generateDownloadToken } from "@/apis/public/fileUpDownload";

/**
 * 获取图片的blob url 需要手动销毁
 * @param fileId 
 * @returns 
 */
export const getBlobUrl = async (fileId: any) => {
    const imageUrl = `/api/starhub/document/download/${fileId}`;
    const token = localStorage.getItem('token');
    const response = await axios.get(imageUrl, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Authorization':`Bearer ${token}`
        },
        responseType: 'blob'
    });
    const imageBlob = await response.request.response; // 将响应体转为 Blob  
    const blobUrl = URL.createObjectURL(imageBlob); // 为 Blob 创建一个 URL 
    return blobUrl;
}

/**
 * 获取下载url 临时token方式
 * @param fileId 
 * @returns 
 */
export const getDownloadUrl = async (fileId: any) => {
    const res = await generateDownloadToken(fileId);
    if(res.status && res.code == 200){
        const token = res.data;
        return `/api/starhub/document/downloadWithToken/${token}`;
    }
    return '';
}
